<template>
	<view class="container" >
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px'}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<text  style="font-size: 15px;" @click="confirm">确认领取</text>
				</view>
			</block>
		</uni-nav-bar>
		
		<uni-section title="样品领取" type="line">
			<view class="example">
				<uni-forms-item label="领样部门" labelPosition="top">
					<uni-easyinput v-model="sample.consumeDepName" disabled=""/>
				</uni-forms-item>
				<uni-forms-item label="领样人" labelPosition="top">
					<uni-combox :candidates="userNames" v-on:input="userChanged" placeholder="请选择领样人"
						v-model="sample.consumerName"></uni-combox>
				</uni-forms-item>
			</view>
		</uni-section>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				sample:{},
				enableRecieveBtn:false,
				showConfirmBtn:false,
				showConfirmModal:false,
				selectedOrgName: '',
				selectedUserName: '',
				orgNames: [],
				orgs: [],
				userNames: [],
				users: [],
				rules: {
					department: {
						rules: [{
							required: true,
							errorMessage: '请选择领样部门',
						}]
					},
					consumer: {
						rules: [{
							required: true,
							errorMessage: '请选择领养人',
						}]
					}
				}
			}
		},
		onLoad(option) {
			// 加载样品存放地点(字典数据，Location)
			this.loadSampleInfo(option.id);
		},
		computed: {},
		methods: {
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					//如果样品领取机构为空,则默认显示为当前用户机构
					if(!this_.sample.consumeDep){
						this_.sample.consumeDep = this_.vuex_user.deptId;
						this_.sample.consumeDepName = this_.vuex_user.dept.deptName;
						this_.sample.consumer = this_.vuex_user.userId;
						this_.sample.consumerName = this_.vuex_user.nickName;
					//如果样品领取人为空,则显示当前用户
					}else if(this_.sample.consumeDep == this_.vuex_user.deptId 
						&& !this_.sample.consume){
						this_.sample.consumer = this_.vuex_user.userId;
						this_.sample.consumerName = this_.vuex_user.nickName;
					}
					this_.queryUserList();
				});
			},
			queryUserList(params) {
				var this_ = this; //存储this
				if(!this_.sample.consumeDep){
					this_.sample.consumeDep = this_.$store.state.vuex_user.deptId;
					this_.sample.consumeDepName = this_.$store.state.vuex_user.dept.deptName;
				}
				this.$u.api.queryOrgUserList(this_.sample.consumeDep).then((response) => {
					//这个控件只能支持字符串选取
					this_.users = response.rows;
					let userNames = [response.rows.length];
					for (let index = 0; index < response.rows.length; index++) {
						userNames[index] = response.rows[index].nickName;
					}
					this_.userNames = userNames;
				});
			},
			confirm(){
				var this_ = this; //存储this
				this_.$u.api.forwardSample(this_.sample).then(res=>{
					if(res.code==200){
						this_.$u.toast('领取成功');
						this_.back();
					}else{
						this_.$u.toast('调用失败,'+res.msg);
						
					}
					
				});
			},
			userChanged(userName) {
				var this_ = this; //存储this
				//如果填入的用户名称非法，则主动清空内容
				var userId = this_.resolveUserIdWithSelectedUserName(userName);

				if (!userId) {
					this_.sample.consumerName='';
				} else {
					this_.sample.consumer = userId;
				}
			},
			resolveUserIdWithSelectedUserName(userName) {
				for (let index = 0; index < this.users.length; index++) {
					if (this.users[index].userName === userName) return this.users[index].userId;
				}
				return null;
			},
			back() {
				this.backPageRefresh();
			}
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
